package com.hs.common.provider

import com.google.gson.Gson
import com.hs.common.dto.SysUserDto
import com.hs.framework.ext.toJson
import com.hs.framework.utils.MMKVUtil

object SysUserProvider {

    val CURRENT_USER_KEY : String = "CURRENT_USER_PROVIDER"

    /**
     * 更新当前用户
     */
    fun updateUser(sysUserDto: SysUserDto){
        sysUserDto?.let {
            var gson = Gson()
            MMKVUtil.putString(CURRENT_USER_KEY,gson.toJson(it))
        }
    }

    /**
     * 获取当前的用户
     */
    fun getCurUser():SysUserDto?{
        val userInfo = MMKVUtil.getString(CURRENT_USER_KEY, "")
        val gson = Gson()
        return userInfo.takeIf { it.isNotBlank() }?.let { gson.fromJson(it, SysUserDto::class.java) }
    }

    /**
     * 移除
     */
    fun removeUser(){
        MMKVUtil.removeKey(CURRENT_USER_KEY)
    }
}